package com.dao.impl;

import com.bean.Hw;
import com.bean.HwStu;
import com.bean.Student;
import com.dao.BaseDao;
import com.dao.HwStuDao;
import com.utils.SqlUtils;

import java.util.List;
import java.util.Map;
public class HwStuDaoImpl extends BaseDao implements HwStuDao {
    String[] strings = {"StudentName","TeacherName","ClassName","HwStuAttachUrl"};

    @Override
    public List<HwStu> selectForPageItems(int begin, int pageSize, String content) {
        if (content == null) {
            content = "";
        }
        content = "%" + content + "%";
        String sql = SqlUtils.pageSql(HwStu.class, Student.class, strings);
        return queryForList(HwStu.class, sql, content, begin, pageSize);
    }

    @Override
    public Integer selectForPageTotalCount(String content) {
        if (content == null) {
            content = "";
        }
        content = "%" + content + "%";
        String sql = SqlUtils.countSql(HwStu.class, Student.class, strings);
        Number count = (Number) queryForSingleValue(sql, content);
        return count.intValue();
    }

    @Override
    public HwStu selectBy(HwStu hwStu) {
        Map.Entry<String, Object[]> entry = SqlUtils.selectBy(hwStu, Student.class, strings);
        return queryForOne(HwStu.class, entry.getKey(), entry.getValue());
    }

    @Override
    public int insert(HwStu hwStu) {
        Map.Entry<String, Object[]> entry = SqlUtils.insertSql(hwStu, strings);
        return update(entry.getKey(), entry.getValue());
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        String sql = "delete from `Hw_Stu` where `id` = ?";
        return update(sql, id);
    }

    @Override
    public int updateByPrimaryKey(HwStu hwStu) {
        Map.Entry<String, Object[]> entry = SqlUtils.updateSql(hwStu, strings);
        return update(entry.getKey(), entry.getValue());
    }

    @Override
    public List<HwStu> approved(int id, int begin, int pageSize) {
        String sql = "select hw.`title` status,Teacher.`name` teacherName,hwStu.`comment`,hwStu.`commentTime`  from hwStu " +
                "left join hw on hwStu.hwId=hw.Id " +
                "left join Teacher on hw.TeacherId=Teacher.id " +
                "where hwStu.studentId=? and not hwStu.`comment` is null order by hw.endTime desc LIMIT ?,?";
        return queryForList(HwStu.class, sql, id, begin, pageSize);
    }

    @Override
    public Integer approvedCount(int id) {
        String sql = "select count(1) from hwStu " +
                "where hwStu.studentId=? and not hwStu.`comment` is null";
        Number count = (Number) queryForSingleValue(sql, id);
        return count.intValue();
    }
}
